//
//  MainUITabBar.swift
//  weibo2
//
//  Created by xuwzh on 15/10/6.
//  Copyright © 2015年 xuwzh. All rights reserved.
//

import UIKit

class MainUITabBar: UITabBar {
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //将按钮均分
        
        let btnCount=5.0
        let w=self.bounds.size.width / CGFloat(btnCount)
        let h=self.bounds.size.height
        let frame = CGRectMake(0, 0, w, h)
        var index=0
        for v in subviews {
           //判断v的类型
            if v is UIControl && !(v is UIButton) {
               v.frame=CGRectOffset(frame, CGFloat (index) * w, 0)
                
                //三目
                
                index += (index==1) ? 2 : 1
            }
           
            
           self.composeButton.frame=CGRectOffset(frame, 2 * w, 0)
         
        }
        
        
    }

    
    ///懒加载实现中间的 按钮
    lazy var composeButton : UIButton = {
         ///实例化一个uibutton
        let btn=UIButton()
       
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        //懒加载的代码是闭包，是提前准备好的代码，在需要的时候执行，闭包中需要使用self
       // self.addSubview(btn)
        self.addSubview(btn)
        return btn
        
        }()
    
}
